package com.umeox.watch.moto.launcher.ui;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.PhoneNumberUtils;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.umeox.moto.common.clicklistener.OnNoDoubleClickListener;
import com.umeox.moto.common.utils.NetUtils;
import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity;
import com.umeox.moto.watch.themes.util.UToast;
import com.umeox.moto.watch.themes.widget.TextView;
import com.umeox.watch.moto.launcher.R;

/**
 * Created by Rambo 2023/7/4
 * Describe:
 */
public class DialPadActivity2 extends ThemeAppCompatActivity implements View.OnClickListener, View.OnLongClickListener {

    private ImageView mDeleteIv;
    private EditText mPhoneNumberTv;
    private ImageView mDialIv;

    private TextView mNumberTv1;
    private TextView mNumberTv2;
    private TextView mNumberTv3;
    private TextView mNumberTv4;
    private TextView mNumberTv5;
    private TextView mNumberTv6;
    private TextView mNumberTvPound;
    private TextView mNumberTv8;
    private TextView mNumberTv9;

    private RelativeLayout mNumber7Rl, mNumber0Rl;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dial_pad2);
        initView();
        initListener();
    }

    private void initView() {
        mNumberTv1 = findViewById(R.id.tv_number1);
        mNumberTv2 = findViewById(R.id.tv_number2);
        mNumberTv3 = findViewById(R.id.tv_number3);
        mNumberTv4 = findViewById(R.id.tv_number4);
        mNumberTv5 = findViewById(R.id.tv_number5);
        mNumberTv6 = findViewById(R.id.tv_number6);
        mNumberTv8 = findViewById(R.id.tv_number8);
        mNumberTv9 = findViewById(R.id.tv_number9);
        mNumberTvPound = findViewById(R.id.tv_number_pound);
        mNumber0Rl = findViewById(R.id.rl_number0);
        mNumber7Rl = findViewById(R.id.rl_number7);

        mDeleteIv = findViewById(R.id.iv_delete);
        mDialIv = findViewById(R.id.iv_dial);
        mPhoneNumberTv = findViewById(R.id.tv_phone_number);


    }

    private void initListener() {
        mNumberTv1.setOnClickListener(this);
        mNumberTv2.setOnClickListener(this);
        mNumberTv3.setOnClickListener(this);
        mNumberTv4.setOnClickListener(this);
        mNumberTv5.setOnClickListener(this);
        mNumberTv6.setOnClickListener(this);
        mNumberTv8.setOnClickListener(this);
        mNumberTv9.setOnClickListener(this);
        mNumberTvPound.setOnClickListener(this);
        mNumber0Rl.setOnClickListener(this);
        mNumber0Rl.setOnLongClickListener(this);
        mNumber7Rl.setOnClickListener(this);
        mNumber7Rl.setOnLongClickListener(this);
        mDeleteIv.setOnClickListener(this);
        mDeleteIv.setOnLongClickListener(this);

        mDialIv.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                String number = mPhoneNumberTv.getText().toString().trim();
                if (!TextUtils.isEmpty(number)) {
                    if (PhoneNumberUtils.isEmergencyNumber(number)) {
                        Intent intent = new Intent("android.intent.action.CALL_PRIVILEGED", Uri.parse("tel:" + number));
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    } else {
                        if (!NetUtils.hasSimCard()) {
                            UToast.showCustomToast(DialPadActivity2.this, getString(R.string.no_simcard_str), Toast.LENGTH_LONG);
                            return;
                        }
                        Intent intent = new Intent("android.intent.action.CALL_PRIVILEGED", Uri.parse("tel:" + number));
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    }
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.tv_number1:
                updatePhoneNumberTv("1");
                break;
            case R.id.tv_number2:
                updatePhoneNumberTv("2");
                break;
            case R.id.tv_number3:
                updatePhoneNumberTv("3");
                break;
            case R.id.tv_number4:
                updatePhoneNumberTv("4");
                break;
            case R.id.tv_number5:
                updatePhoneNumberTv("5");
                break;
            case R.id.tv_number6:
                updatePhoneNumberTv("6");
                break;
            case R.id.tv_number8:
                updatePhoneNumberTv("8");
                break;
            case R.id.tv_number9:
                updatePhoneNumberTv("9");
                break;
            case R.id.tv_number_pound:
                updatePhoneNumberTv("#");
                break;
            case R.id.rl_number0:
                updatePhoneNumberTv("0");
                break;
            case R.id.rl_number7:
                updatePhoneNumberTv("7");
                break;
            case R.id.iv_delete:
                if (!TextUtils.isEmpty(mPhoneNumberTv.getText().toString().trim())) {
                    mPhoneNumberTv.setText(mPhoneNumberTv.getText().subSequence(0, mPhoneNumberTv.getText().length() - 1));
                }
                break;
        }
    }

    private void updatePhoneNumberTv(String number) {
        mPhoneNumberTv.setText(String.format("%s%s", mPhoneNumberTv.getText(), number));
        mPhoneNumberTv.setSelection(mPhoneNumberTv.getText().length());
    }

    @Override
    public boolean onLongClick(View v) {
        if (v.getId() == R.id.iv_delete) {
            if (!TextUtils.isEmpty(mPhoneNumberTv.getText())) {
                mPhoneNumberTv.setText("");
            }
            return true;
        } else if (v.getId() == R.id.rl_number0) {
            updatePhoneNumberTv("+");
            return true;
        } else if (v.getId() == R.id.rl_number7) {
            updatePhoneNumberTv("*");
            return true;
        }
        return false;
    }
}
